<?php
/**
 * 接口应用管理
 * @desc 增加appid，secretkey及权限设置类
 */
class Api_Application extends PhalApi_Api {

    public function getRules() {
        return [
            '*'      => [
                'token' => ['name' => 'token', 'type' => 'string', 'min' => 12, 'max' => 12, 'require' => true, 'desc' => 'tokenID'],
            ],
            'rules'  => [],
            'add'    => [
                'rules' => ['name' => 'rules', 'type' => 'array', 'require' => true, 'desc' => '权限'],
            ],
            'update'   => [
                'id'    => ['name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '应用id'],
                'rules' => ['name' => 'rules', 'type' => 'array', 'require' => true, 'desc' => '权限'],
            ],
            'resetSecretKey'   => [
                'id'    => ['name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '应用id'],
            ],
            'lists'  => [
            ],
            'delete' => [
                'id' => ['name' => 'id', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '应用id'],
            ],
            'info'   => [
                'id' => ['name' => 'id', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '应用id'],
            ]

        ];
    }


    /**
     * 应用权限列表
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return array rules 应用权限列表
     * @return string msg 提示信息
     */
    public function rules() {

        $db = new Domain_Application();
        return [
            "rules" => $db->rule_config()
        ];
    }
    /**
     * 应用秘钥重置
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return string msg 提示信息
     */
    public function resetSecretKey(){
        $db = new Domain_Application();
        $data=$db->resetSecretKey($this->id);
        if(!$data){
            throw new PhalApi_Exception('重置失败', 1006);
        }
        return $data;
    }
    /**
     * 应用添加
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return object data 添加后的数据
     * @return string msg 提示信息
     */
    public function add() {

        $db = new Domain_Application();
        $rules = $this->rules;

        if (count($rules) == 0) {

            throw new PhalApi_Exception('不能添加无权限应用账号', 1004);
        }
        $data = $db->add(@json_encode($rules));
        if (!$data) {
            throw new PhalApi_Exception('添加账号失败', 1005);
        }
        $data["rules"] = @json_decode($data["rules"]);
        return $data;

    }

    /**
     * 更新应用资料
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int code
     * @return boolean data.isUpdated 是否更新成功
     * @return string msg 提示信息
     */
    public function update() {


        $db = new Domain_Application();
        $hasid = $db->info($this->id);

        if (!$hasid) {
            throw new PhalApi_Exception("错误的id", 40002);
        }

        $rules = $this->rules;

        if (count($rules) == 0) {

            throw new PhalApi_Exception('不能添加无权限应用账号', 1004);
        }

        return [
            "isUpdated" => $db->update(@json_encode($rules), $this->id)
        ];

    }

    /**
     * 应用资料
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int id id
     * @return string appid appid
     * @return string SecretKey 秘钥
     * @return array rules 权限
     * @return string msg 提示信息
     */

    public function info() {
        $db = new Domain_Application();
        return $db->info($this->id);
    }

    /**
     * 应用删除
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return boolean data.isdelete 是否删除成功
     * @return string msg 提示信息
     */
    public function delete() {
        $db = new Domain_Application();
        return [
            "isdelete" => ($db->delete($this->id) == 1)
        ];
    }


    /**
     * 应用列表
     * @desc 返回数据参见应用资料接口
     * @return int ret HTTP状态码,200成功,其他失败
     * @return array lists 数据列表
     * @return int total 数据总数
     * @return string msg 提示信息
     */
    public function lists() {
        $db = new Domain_Application();

        return $db->getlist();
    }

}
